<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="参数管理">
          <el-input v-model="searchForm.paramKey" placeholder="参数名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"-->
    <el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection">
      </el-table-column>
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>
      <el-table-column prop="paramKey" label="参数键" align="center">
      </el-table-column>
      <el-table-column prop="paramValue" label="参数值" show-overflow-tooltip align="center">
      </el-table-column>
      <el-table-column prop="type" label="参数类型" align="center">
        <template scope="scope">
          <span v-if="scope.row.type" style="color: green">普通表达项</span>
          <span v-else-if="!scope.row.type" style="color: red">文件上传表单项</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" align="center">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" align="center">
      </el-table-column>
      <el-table-column prop="operatorName" label="操作人" align="center">
      </el-table-column>
      <el-table-column prop="paramDesc" label="参数描述" align="center">
      </el-table-column>


      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>
    <!--    :rules="saveFormRules" 规则-->
    <!--添加或修改界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" ref="saveForm">
        <el-form-item label="参数键">
          <el-input v-model="saveForm.paramKey"></el-input>
        </el-form-item>
        <el-form-item label="参数值">
          <el-input v-model="saveForm.paramValue"></el-input>
        </el-form-item>
        <el-form-item label="参数类型">
          <el-input v-model="saveForm.type"></el-input>
        </el-form-item>
        <el-form-item label="操作人">
          <el-input v-model="saveForm.operatorName"></el-input>
        </el-form-item>
        <el-form-item label="参数描述">
          <el-input v-model="saveForm.paramDesc"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

import {format} from "echarts/lib/export";
import jwt from 'jsonwebtoken';
export default {
  data() {

    return {

      sels: [],
      title: "",
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 10, //每页显示多少条
      tableDatas: [], //当前页数据
      listLoading: false,

      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //5.添加和修改相关数据
      saveFormVisible: false,
      saveForm: {
        id: null,
        paramKey: "", //参数键
        paramValue: "", //参数值
        type: "", //参数类型(0:普通表达项,1:文件上传表单项)
        createTime: "", //添加时间
        updateTime: "", //修改时间
        operatorName: "", //操作人
        paramDesc: "", //参数描述
      },
      searchForm: {
        paramKey: "",
      }


    }
  },


  methods: {

    //1.分页查询相关方法
    //1.1.获取列表数据
    getTableData() {
      //开启加载框
      this.listLoading = true;
      let param = {
        "currentPage": this.currentPage,
        "pageSize": this.pageSize,
        "key": this.searchForm.paramKey,
      };
      this.$http.patch("/param", param).then(res => {
        let {success, data} = res.data;
        if (success) {
          //关闭加载
          this.listLoading = false;
          this.tableDatas = data.list
          this.totals = data.total;
        }
      })

    },
    //1.2.选择第几页时触发
    handleCurrentChange(curentPage) {
      this.currentPage = curentPage;
      this.getTableData();
    },
    //1.3.选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.currentPage = 1;
      this.pageSize = pageSize;
      this.getTableData();
    },

    //2.高级查询相关方法
    keywordQuery() {
      this.currentPage = 1;
      this.getTableData();


    },

    //3.删除相关方法
    handleDel(row) {
      //console.log(row)
      this.$confirm('是否确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete("/param/" + row.id).then(res => {
          //console.log(res)
          let {success} = res.data;
          if (success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
          this.getTableData();
        })
      });
    },


    //4.批量删除相关方法
    //4.1.选项改变时触发
    handleSelectionChange(val) {
      this.sels = val;
      //console.log(val)
    },
    //4.2.批量删除
    handlePatchDel() {
      this.$confirm('是否确认批量删除?', '批量删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.patch("/param/batchDeletion", this.sels).then(res => {
          //console.log(res)
          let {success} = res.data;
          if (success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
          this.getTableData();
        })
      });
    },

    //5.添加和修改相关方法
    //5.1.点击添加弹出对话框
    handleAdd() {
      this.saveFormVisible = true;
      //标题
      this.title = "添加权限管理";
      for (let k in this.saveForm) {
        //清空表单
        this.saveForm[k] = "";
      }

    },

    //5.2.点击修改弹出对话框
    handleEdit(row) {
      //console.log(row.articlePic)
      //标题
      this.title = "修改权限管理";
      this.saveFormVisible = true;
      for (let k in this.saveForm) {
        this.saveForm[k] = "";
      }
      //数据回显
      this.saveForm = Object.assign({}, row)
      //菜单回显
      this.saveForm.menusId = row.menus.map(m => m.id)
      //权限回显
      this.saveForm.permissionsId = row.permissions.map(p => p.id)
    },


    //5.3.提交表单数据操作数据库
    saveSubmit() {
      this.$http.post("/param", this.saveForm).then(res => {
        //console.log(this.saveForm)
        let {success} = res.data;
        if (success) {
          this.saveFormVisible = false;
          this.getTableData();
        }
      })
    },

    //获取菜单树形
    getMenutree() {
      this.$http.patch("/menu/gettree").then(res => {
        let {success, data} = res.data;
        //console.log(res.data)
        if (success) {
          this.menus = data;
        }
      })
    },
    //获取权限树形
    getPermissiontree() {
      this.$http.patch("/permission/gettree").then(res => {
        let {success, data} = res.data;
        //console.log(res.data)
        if (success) {
          this.permissions = data;
        }
      })
    }

  },
  mounted() {

    this.getTableData();
    this.getMenutree();
    this.getPermissiontree();
  }
}

</script>

<style scoped></style>
